var app = getApp()
var verify = require('../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    locationList:[1,2,1,2,1,2],
    size:10,
    loading: true,
    filterValue: "",
    isFresh: false,
    inputShowed: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var locationList = []
    var size = this.data.size
    var filterValue = this.data.filterValue
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth
    })

    wx.request({
      url: baseUrl + 'locations2?page=0&size='+size+
            '&filter=locationName'+
            '&filterValue=' + filterValue,
      header: {
        'AUTHORIZATION': token
      },
      success(res){
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          // console.log("regionList", res)
          var regionList = res.data.data.content
          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > 10) {
            loading = true
          }
          that.setData({
            regionList: regionList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
        
      }
    })
  },

  // 跳转点位设备页面
  locationDevice:function(res){
    console.log("locationDevice", res.currentTarget.dataset.locationid)
    var locationid = res.currentTarget.dataset.locationid
    wx.navigateTo({
      url: 'locationDevice/locationDevice?locationid='+locationid,
    })
  },
  // 跳转点位设备页面
  locationMap: function (res) {
    console.log("locationMap", res.currentTarget.dataset.locationid)
    var locationId = res.currentTarget.dataset.locationid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this
    wx.request({
      url: baseUrl + '/location/' + locationId,
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          console.log("locationInfo = ", res.data.data)
          var locationInfo = res.data.data
          var locationName = locationInfo.locationName
          var address = locationInfo.address
          var latitude = locationInfo.latitude
          var longitude = locationInfo.longitude
          wx.openLocation({
            latitude: latitude,
            longitude: longitude,
            scale: 18,
            name: locationName,
            address: address,
            complete: function () {
              console.log('complete', arguments);
            }
          });
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },
  // 添加点位
  addRegion:function(){
    wx.navigateTo({
      url: 'addRegion/addRegion',
    })
  },
  // 编辑点位
  editLocation:function(res){
    console.log(res)
    var companyname = res.currentTarget.dataset.companyname
    var locationname = res.currentTarget.dataset.locationname
    var province = res.currentTarget.dataset.province
    var city = res.currentTarget.dataset.city
    var area = res.currentTarget.dataset.area
    var detailaddr = res.currentTarget.dataset.detailaddr
    var public1 = res.currentTarget.dataset.public
    var address = res.currentTarget.dataset.address
    var locationid = res.currentTarget.dataset.locationid
    var regionid = res.currentTarget.dataset.regionid;
		var careless = JSON.parse(res.currentTarget.dataset.careless) ;
    wx.navigateTo({
      url: 'editRegion/editRegion?companyname=' + companyname + 
              '&locationname=' + locationname + 
              '&province=' + province + 
              '&city=' + city + 
              '&area=' + area + 
              '&detailaddr=' + detailaddr + 
              '&public=' + public1 + 
              '&address=' + address + 
              '&locationid=' + locationid + 
              '&regionid=' + regionid +
							'&careless=' + careless
    })
  },
  // 删除点位
  deleteLocation:function(res){
    var locationid = res.currentTarget.dataset.locationid
    var locationName = res.currentTarget.dataset.locationname
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    wx.showModal({
      title: '提示',
      content: '确定删除该点位？\n点位名称：' + locationName,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: baseUrl + 'location/' + locationid ,
            method: "delete",
            header: {
              'AUTHORIZATION': token
            },
            success: function (res) {
              console.log("success", res)
              if (res.data.code == 60006) {
                verify.clearAll()
              } else if (res.data.code == 200) {
                wx.showToast({
                  title: '删除成功!',
                  duration: 3500
                })
                that.onLoad()
              } else {
                wx.showModal({
                  title: '删除失败！',
                  content: res.data.message,
                })
              }
            }
          })
        }
        else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  // 搜索相关
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  // 取消搜索
  hideInput: function () {
    this.setData({
      inputShowed: false,
      filterValue: '',
      inputVal: ''
    });
    this.search();
  },
  inputTyping: function (e) {
    this.setData({
      filterValue: e.detail.value
    });
    this.search();
  },
// 搜索框输入
  input: function (res) {
    this.setData({
      filterValue: res.detail.value
    })
  },
  // 查询点位
  search: function () {
    var filterValue = this.data.filterValue
    var size = 10
    this.setData({
      filterValue: filterValue,
      size: size
    })
    this.onLoad()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var size = this.data.size * 1 + 10
    var loading = this.data.loading
    var totalElements = this.data.totalElements
    this.setData({
      isFresh: true
    })
    if (size - 10 > totalElements) {
      this.setData({
        loading: false
      })
    } else {
      this.setData({
        size: size
      })
      this.onLoad()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})